package tech.yunjing.train.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tech.yunjing.train.service.exception.RedPacketNameException;
import tech.yunjing.train.service.exception.RedisKeyException;
import tech.yunjing.train.service.exception.SmallRedPacketException;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 该类是小红包的添加删除查找类
 * 之前配置过hashOperations
 * 现在只需要根据 对应业务调用其底层对应的方法即可
 * Created by 刘飞 on 2017-6-8.
 */

/**
 * 该类是小红包的添加删除查找类
 * 之前配置过hashOperations
 * 现在只需要根据 对应业务调用其底层对应的方法即可
 * Created by 刘飞 on 2017-6-8.
 */
@Service
public  class SmallRedPacketServiceimp<T>{
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private HashOperations<String,String,T> hashOperations;

    public void put(String key,T redis ,Long expire,String redPacketName){
        if(key==null||key.trim().isEmpty()){
            throw new RedisKeyException("小红包的id不能为空");
        }
        if(redPacketName==null||redPacketName.trim().isEmpty()){
            throw new RedPacketNameException("红包的名称不能为空");
        }
        if(redis==null){
            throw new SmallRedPacketException("请存入正确的对象");
        }
        hashOperations.put(redPacketName,key,redis);
        if(expire!=-1){
            redisTemplate.expire(redPacketName,expire, TimeUnit.SECONDS);
        }
    }

    public void remove(String key,String redPacketName){
        if(key==null||key.trim().isEmpty()){
            throw new RedisKeyException("小红包的id不能为空");
        }
        if(redPacketName==null||redPacketName.trim().isEmpty()){
            throw new RedPacketNameException("大红包的名称不能为空");
        }
        T redis= hashOperations.get(key,redPacketName);
        if(redis==null){
            throw new SmallRedPacketException("请输入正确的表名和id该对象不存在");
        }
        List<T> list=hashOperations.values(redPacketName);
        if(list.isEmpty()){
            throw new RedPacketNameException("请输入正确的表名");
        }
        hashOperations.delete(redPacketName,key);
    }

    public T findOne(String key,String redPacketName){
        if(key==null||key.trim().isEmpty()){
            throw new RedisKeyException("小红包的id不能为空");
        }
        if(redPacketName==null||redPacketName.trim().isEmpty()){
            throw new RedPacketNameException("红包的名称不能为空");
        }
        List<T> list=hashOperations.values(redPacketName);
        if(list.isEmpty()){
            throw new RedPacketNameException("请输入正确的表名");
        }

        return     hashOperations.get(redPacketName,key);
    }

    public List<T> findall(String redPacketName) {

        if(redPacketName==null||redPacketName.trim().isEmpty()) {
            throw new RedPacketNameException("红包名不能为空");
        }


        return hashOperations.values(redPacketName);
    }

    public long count(String redPacketName){

        if(redPacketName==null||redPacketName.trim().isEmpty()) {
            throw new RedPacketNameException("红包名不能为空");
        }
        List<T> list=hashOperations.values(redPacketName);
        if(list.isEmpty()){
            throw new RedPacketNameException("请输入正确的红包名");
        }
        return hashOperations.size(redPacketName);
    }

    public void deleteall(String redPacketName){
        if(redPacketName==null||redPacketName.trim().isEmpty()) {
            throw new RedPacketNameException("红包名不能为空");
        }
        List<T> list=hashOperations.values(redPacketName);
        if(list.isEmpty()){
            throw new RedPacketNameException("请输入正确的红包名");
        }
        Set<String> set=hashOperations.keys(redPacketName);
        for(String i:set){
            hashOperations.delete(redPacketName,i);
        }
    }
}
